package com.example.mybatis.rest;

import com.example.mybatis.dao.OutputDao;
import com.example.mybatis.moudle.Output;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 支出表
 */
@RestController
@RequestMapping("/output")
public class OutputRest {

    @Autowired
    SqlSessionFactory factory;


    /**
     * 根据主键删除一条记录
     * @param id
     */
    @GetMapping("deleteById/{id}")
    public void deleteByPrimaryKey(@PathVariable("id") int id){
        SqlSession sess=factory.openSession();
        OutputDao outputDao=sess.getMapper(OutputDao.class);
        outputDao.deleteByPrimaryKey(id);
        sess.commit();
        sess.close();
    }

    /**
     * 输出所有
     * http://localhost:9090/output/findAll
     * @return
     */
    @GetMapping("findAll")
    public List<Output> findAll(){
        SqlSession sess=factory.openSession();
        OutputDao outputDao=sess.getMapper(OutputDao.class);
        List<Output> list=outputDao.findAll();
        System.out.println(list);
        sess.close();
        if(list==null){
            Output output=new Output();
            list.add(output);
            return list;
        }
        return list;
    }

    /**
     * 按主键查找
     * http://localhost:9090/output/selectById/1
     * @param outputId 主键
     * @return
     */
    @GetMapping("selectById/{id}")
    public Output selectById(@PathVariable("id") int outputId){
        SqlSession sess=factory.openSession();
        OutputDao outputDao=sess.getMapper(OutputDao.class);
        Output output=outputDao.selectByPrimaryKey(outputId);
        sess.close();
        return output;
    }

    /**
     * 按月查找
     *http://localhost:9090/output/selectByMonth/2020/11
     * @param month
     * @return
     */
    @GetMapping("selectByMonth/{year}/{month}")
    public List<Output> selectByMonth(@PathVariable("year") int year,@PathVariable("month") int month){
        SqlSession sess=factory.openSession();
        OutputDao outputDao=sess.getMapper(OutputDao.class);
        List<Output> output=outputDao.selectByMonth(year,month);
        sess.close();
        return output;
    }

    /**
     * 按年查找
     * http://localhost:9090/output/selectByYear/2020
     * @param year
     * @return
     */
    @GetMapping("selectByYear/{year}")
    public List<Output> selectByYear(@PathVariable("year") int year){
        SqlSession sess=factory.openSession();
        OutputDao outputDao=sess.getMapper(OutputDao.class);
        List<Output> output=outputDao.selectByYear(year);
        sess.close();
        return output;
    }

    /**
     * 按照支付方式(支付宝,微信...)查找
     * http://localhost:9090/output/selectByMethod/微信
     * @param method
     * @return
     */
    @GetMapping("selectByMethod/{method}")
    public List<Output> selectByMethod(@PathVariable("method") String method){
        SqlSession sess=factory.openSession();
        OutputDao outputDao=sess.getMapper(OutputDao.class);
        List<Output> output=outputDao.selectByMethod(method);
        sess.close();
        return output;
    }

    /**
     * 按种类(零食,水果...)查找
     * http://localhost:9090/output/selectByKind/零食
     * @param kind
     * @return
     */
    @GetMapping("selectByKind/{kind}")
    public List<Output> selectByKind(@PathVariable("kind") String kind){
        SqlSession sess=factory.openSession();
        OutputDao outputDao=sess.getMapper(OutputDao.class);
        List<Output> output=outputDao.selectByKind(kind);
        sess.close();
        return output;
    }

    /**
     * 新增一条记录
     * http://localhost:9090/output/insertOutput
     * @param map
     * @return
     */
    @PostMapping("/insertOutput")
    @ResponseBody
    public Output insertOutput(@RequestBody Map map) {
        try {
            String id=map.get("outputId").toString();
            String count=map.get("outputCount").toString();
            String date=map.get("outputDate").toString();
            Date date1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
            String kind=map.get("outputKind").toString();
            String method=map.get("outputMethod").toString();
            String text=map.get("outputText").toString();
            Output output = new Output(Integer.parseInt(id),
                    Double.parseDouble(count),
                    date1,
                    kind, method, text);
            System.out.println(output);
            SqlSession sess=factory.openSession();
            OutputDao outputDao=sess.getMapper(OutputDao.class);
            outputDao.insertSelective(output);
            sess.commit();
            sess.close();
            System.out.println(output);
            return output;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        finally {
            return null;
        }


    }


    /**
     * 根据主键更新一条记录
     * http://localhost:9090/output/updateOutput
     * @param output
     * @return
     */
    @PostMapping("/updateOutput")
    @ResponseBody
    public Output updateOutput(@RequestBody Output output) {
        try {
            System.out.println(output);
            SqlSession sess=factory.openSession();
            OutputDao outputDao=sess.getMapper(OutputDao.class);
            outputDao.updateByPrimaryKeySelective(output);
            sess.commit();
            sess.close();
            return output;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        finally {
            return null;
        }


    }
}
